package com.naiterui.ehp.ps.push.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.ps.push.bean.MessageRegist;
import com.naiterui.ehp.ps.push.exception.ExceptionCodes;
import com.naiterui.ehp.ps.push.service.IMessageRegistService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("push/register")
@Api(tags = {SwaggerTag.PUSH + SwaggerTag.PUSH_MESSAGE})
public class MessageRegistController {

    /**
     * 日志对象 ：<code>logger</code>
     */
    private static final Logger logger = LoggerFactory.getLogger(MessageRegistController.class);

    @Autowired
    private IMessageRegistService messageRegistService;

    /**
     * 注册
     * MessageRegistController.register()
     *
     * @param client
     * @param type
     * @param requestUri
     *
     * @return
     *
     * @Date 2016年3月30日
     * @since 2.3.0
     */
    @RequestMapping("register")
    @ResponseBody
    public void register(Integer client, Integer type, String requestUri, String userType) throws BusinessException {
        // 空参校验
        if (null == client || null == type || null == requestUri) {
            logger.error("messageRegister register ERROR ; illegal param : client={},type={},requestUri={},userType={}",
                         client, type, requestUri, userType);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        this.messageRegistService.registMessage(client, type, requestUri, userType);
    }

    /**
     * 删除
     * MessageRegistController.removeRegister()
     *
     * @param client
     * @param type
     * @param requestUri
     *
     * @return
     *
     * @Date 2016年4月6日
     * @since 2.3.0
     */
    @RequestMapping("remove")
    @ResponseBody
    public void removeRegister(Integer client, Integer type, String requestUri) throws BusinessException {
        // 空参校验
        if (null == client || null == type || StringUtils.isEmpty(requestUri)) {
            logger.error("messageRegister removeRegister ERROR ; illegal param : client={},type={},requestUri={}", client, type,
                         requestUri);
        }

        this.messageRegistService.removeMessage(client, type, requestUri);
    }

    /**
     * 消息类型列表
     * MessageRegistController.registerList()
     *
     * @return
     *
     * @Date 2016年4月8日
     * @since 2.3.0
     */
    @RequestMapping("list")
    @ResponseBody
    public List<MessageRegist> registerList() {
        return this.messageRegistService.getMessageRegister();
    }
}
